package com.youway.ddd.demo2.domain.service;

import java.util.Optional;

import javax.annotation.Resource;

import com.youway.ddd.demo2.application.service.BookshelfSpaceService;
import com.youway.ddd.demo2.domain.entity.Book;
import com.youway.ddd.demo2.domain.entity.Bookshelf;
import com.youway.ddd.demo2.domain.entity.BookshelfSpace;
import com.youway.ddd.demo2.domain.repository.BookRepository;
import com.youway.ddd.demo2.domain.repository.BookshelfRepository;
import com.youway.ddd.demo2.domain.repository.BookshelfSpaceRepository;
import com.youway.ddd.demo2.infrastructure.base.resp.RespBody;

/**
 * 领域对象 完成 application层 委托的功能  (与领域服务不是同一概念 本例中没有领域服务案例)
 */
public class BookshelfSpaceDomain implements BookshelfSpaceService{
	
	@Resource
	private BookRepository bookRepository;
	
	@Resource
	private BookshelfRepository bookshelfRepository;
	
	
	@Resource
	private BookshelfSpaceRepository bookshelfSpaceRepository;
	
    public RespBody<Book> register(String title, double price) {
    	Book book = new Book();
        book.setTitle(title);
        book.setPrice(price);
        Book entity = bookRepository.save(book);
        if (entity == null) return RespBody.ok(null);
        return RespBody.ok(entity);
    }
    
    @Override
    public RespBody<Bookshelf> addBookshelf(String category) {
        Bookshelf bookshelf = new Bookshelf();
        bookshelf.setCategory(category);
        Bookshelf entity = bookshelfRepository.save(bookshelf);
        if (entity == null) return RespBody.ok(null);
        return RespBody.ok(entity);
    }
    
    @Override
    public RespBody<BookshelfSpace> getBookshelfSpace(Long bookshelfSpaceId) {
    	BookshelfSpace entity = bookshelfSpaceRepository.getOne(bookshelfSpaceId);
    	if (entity == null) return RespBody.ok(null);
        return RespBody.ok(entity);
    }

    @Override
    public RespBody<Bookshelf> findShelf(String category) {
    	Bookshelf entity = bookshelfRepository.findByCategory(category);
        if (entity == null) return RespBody.ok(null);
        return RespBody.ok(entity);
    }
}
